<?php
session_start();
include_once('stationInfo.php');
include_once('config.php');

	//Main action to perform from hidden form varaibles
	$action = $_POST['action'];
	//+++++++++++++++++++++++++
	//BEGIN ENTER NEW UNDER WRITER BLOCK
	//+++++++++++++++++++++++++
	if($action == 'enter'){
		$name = mysql_real_escape_string ($_POST['name']);
		$abbreviation = mysql_real_escape_string ($_POST['abbreviation']);

		if(!$name || !$abbreviation){
			$_SESSION['message'] = "U.W. not added.  Please fill in all fields!";
			header("Location:index.php?ID=0&subMenu=0&action=enter");
		}else{

		$sql = "SELECT * FROM $underwriter_Table WHERE abbreviation = '$abbreviation'";
		$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
		 if (DB::isError($result)) {
		 die ($result->getMessage());
		 }


		$sql = "SELECT * FROM $underwriter_Table WHERE name = '$name'";
		$result1 = $db->getAll($sql, DB_FETCHMODE_ASSOC);
		 if (DB::isError($result1)) {
		 die ($result1->getMessage());
		 }

		$num = count($result);
		$num1 = count($result1);

			if($num != '0'){
			$_SESSION['message'] = "RECORD NOT ADDED. The abbreviation you entered is already in use!";
			header("Location:index.php?ID=0&subMenu=0&action=enter");
			}else if($num1 != '0'){
			$_SESSION['message'] = "RECORD NOT ADDED. The name you entered is already in use!";
			header("Location:index.php?ID=0&subMenu=0&action=enter");
			}else{

			$sql = "INSERT INTO $underwriter_Table values ('0','$name','$abbreviation')";
			$result = $db->query($sql);
			if (DB::isError($result)) {
				die ($result->getMessage());
			}
			$_SESSION['message'] = "New Underwriter successfully Added!";
			header("Location:index.php?ID=0&subMenu=0&action=enter");
			}
		}

		//echo $_SESSION['message'];
	}
	//+++++++++++++++++++++++++
	//END ENTER NEW UNDER WRITER BLOCK
	//+++++++++++++++++++++++++

	//+++++++++++++++++++++++++
	//BEGIN SEARCH ALL UNDERWRITERS BLOCK
	//+++++++++++++++++++++++++
	if($action == 'search'){
		$searchName = mysql_real_escape_string (stripslashes($_POST['name']));
		$searchfield = $_POST['searchfield'];
		//$singleSearch = $_POST['singleSearch'];

		//if($singleSearch == 'true'){
			//$sql = "SELECT ID FROM $underwriter_Table WHERE name LIKE '%$searchName%'";
			//$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
			// if (DB::isError($result)) {
			 //die ($result->getMessage());
			 //}
			// $_SESSION['searchSubscriptions'] = 'true';
			// $_SESSION['updateArray'][] = $result[0][0]['ID']
		//}else{//send result to "List Underwriters Form"
			$sql = "SELECT * FROM $underwriter_Table WHERE $searchfield LIKE '%$searchName%' ORDER BY name asc";
			$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
			if (DB::isError($result)) {
			die ($result->getMessage());
			}
		//}


		if(count($result) == '0'){
			$_SESSION['message'] = "No Matches Found!";
			header("Location:index.php?ID=0&subMenu=0&action=search");
		}else{
			$_SESSION['result'] = $result;
			$_SESSION['highNumber'] = $num;
			$_SESSION['message'] = "Here are your search results!";
			header("Location:index.php?ID=0&subMenu=0&action=list");
		}

	}
	//+++++++++++++++++++++++++
	//END SEARCH ALL UNDERWRITERS BLOCK
	//+++++++++++++++++++++++++

	//+++++++++++++++++++++++++
	//BEGIN LIST ALL UNDERWRITERS BLOCK
	//+++++++++++++++++++++++++
	if($action == 'list'){
	$highnumber = $_POST['highnumber'];
	$_SESSION['updateArray'][] = array();
	$i=0;
	$q = 0;
	while($i < $highnumber){
	$processID = $_POST["radioGroup$i"];
		if($processID){
		$haveProcessedSomething = 'true';
		$action = substr("$processID", 0, (strpos($processID, ':')));
		$actionID = substr("$processID", ((strpos($processID, ':'))+1), strlen ("$processID"));
			//Delete Selected entries+++++
			if($action == 'delete'){
			$haveDeletedSomething = 'true';

			$result[] = $db->query("DELETE from $underwriter_Table WHERE ID = '$actionID'");
			}else if ($action == 'update'){
			$haveUpdatedSomething = 'true';
			//add entries to update to an array
			$_SESSION['updateArray']["$q"] = $actionID;
			++$q;
			}

	}

	++$i;
	}
	if($haveProcessedSomething != 'true'){
	$_SESSION['message'] = "NO ACTION TAKEN. You must select underwriter(s) to update or delete!";
	header("Location:index.php?ID=0&subMenu=0&action=list");

	}else{
		if($haveDeletedSomething == 'true' && $haveUpdatedSomething != 'true'){
			$_SESSION['message'] = "The selected underwriter(s) have been removed!";
			header("Location:index.php?ID=0&subMenu=0&action=list");
		}else if($haveUpdatedSomething == 'true'){
		$_SESSION['searchSubscriptions'] = 'true';
		//$_SESSION['highNumber'] = $i;
		$_SESSION['message'] = "Here are the underwriter(s) to update!";
		header("Location:index.php?ID=0&subMenu=0&action=modify");
		//echo count($_SESSION['updateArray']);
		}

	}
	//end main if statement for List functions
	}
	//+++++++++++++++++++++++++
	//END LIST ALL UNDERWRITERS BLOCK
	//+++++++++++++++++++++++++


	//+++++++++++++++++++++++++
	//BEGIN MODIFY UNDERWRITERS BLOCK
	//+++++++++++++++++++++++++
	if($action == 'modify'){
		$highnumber = $_POST['highnumber'];
		$_SESSION['updateArray'][] = array();

		/* $ATQuery = "SELECT * from $airtimes_Table ORDER BY military";
		$resultAT = $db->getAll($ATQuery, DB_FETCHMODE_ASSOC);
		if (DB::isError($resultAT)) {
		die ($resultAT->getMessage());
		} */





		$i = 0;
		while($i < $highnumber){
		$ID = mysql_real_escape_string ($_POST["ID$i"]);
		$name = mysql_real_escape_string (stripslashes ($_POST["name$i"]));
		$abbreviation = mysql_real_escape_string (stripslashes ($_POST["abbreviation$i"]));
		$originalAbbreviation = $_POST["originalAbbreviation$i"];

		if($_POST['historical'] == 'true'){
		$sql = "SELECT ID, spotID FROM $grid_Table WHERE spotID LIKE '$originalAbbreviation%'";
		$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
			if (DB::isError($result)) {
		    die ($result->getMessage());
			}
		}


		$origLength = strlen($originalAbbreviation);

		$alldata = array();

		for ($r = 0; $r < count($result); $r++) {
		$extension = substr($result[$r]['spotID'], $origLength, strlen($result[$r]['spotID']));
		$newSpotID = $abbreviation . $extension;
		$gridID = $result[$r]['ID'];
		$alldata[] = array($name,$abbreviation,$newSpotID,$gridID);
		  }
		$sth = $db->prepare("UPDATE $grid_Table SET underwriter = ?,abbreviation = ?,spotID = ? WHERE ID = ?");
		$db->executeMultiple($sth, $alldata);


		$Query = "UPDATE $underwriter_Table SET name = '$name',abbreviation = '$abbreviation' WHERE ID = '$ID'";
		$result = $db->query($Query);
		$_SESSION['updateArray'][$i] = $ID;
		++$i;
		}
		$_SESSION['searchSubscriptions'] = 'false';
		$_SESSION['message'] = "The selected underwriter(s) have been updated!";
		header("Location:index.php?ID=0&subMenu=0&action=modify");

	}
	//+++++++++++++++++++++++++
	//END MODIFY UNDERWRITERS BLOCK
	//+++++++++++++++++++++++++


 	//##############################################################################################################
	//BEGIN MANAGE AIRTIMES CODE BLOCKS
	//##############################################################################################################


//++++++++++++++++++++++++++++
//BEGIN UPDATE AIRTIMES IN DB
//+++++++++++++++++++++++++++
if($action == 'updateairtime'){
	$alldata = array();

	$i=0;
	while ($i < $_POST['highnumber']){
		if($_POST["ID$i"]){
		$MyID = $_POST["ID$i"];
		$hour = $_POST["hour$i"];
		$minute = $_POST["minute$i"];
		$seconds = $_POST["seconds$i"];
		$ampm = $_POST["ampm$i"];

		if(!$_POST["mon$i"]){
			$mon = 'false';
		}else{
			$mon = $_POST["mon$i"];
		}


		if(!$_POST["tue$i"]){
			$tue = 'false';
		}else{
			$tue = $_POST["tue$i"];
		}


		if(!$_POST["wed$i"]){
			$wed = 'false';
		}else{
			$wed = $_POST["wed$i"];
		}


		if(!$_POST["thu$i"]){
			$thu = 'false';
		}else{
			$thu = $_POST["thu$i"];
		}


		if(!$_POST["fri$i"]){
			$fri = 'false';
		}else{
			$fri = $_POST["fri$i"];
		}


		if(!$_POST["sat$i"]){
			$sat = 'false';
		}else{
			$sat = $_POST["sat$i"];
		}

		if(!$_POST["sun$i"]){
			$sun = 'false';
		}else{
			$sun = $_POST["sun$i"];
		}



		calcMilitary($hour,$minute,$seconds,$ampm);

		$alldata[] = array($hour,$minute,$seconds,$ampm,$military,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$MyID);

					//+++++++++++++++++++++++++++++++++++++++++++++++++++++
					//BEGIN DELETE AIRTIMES FROM EXISTING GRID ENTRIES+++++
					//+++++++++++++++++++++++++++++++++++++++++++++++++++++
							$result = array();
							$sth = "SELECT military FROM $airtimes_Table WHERE ID = '$MyID'";
							$result = $db->getAll($sth,DB_FETCHMODE_ASSOC);
							if (DB::isError($result)) {
							die ($result->getMessage());
							}


					//+++++++++++++++++++++++++++++++++++++++++++++++++++++
					//BEGIN DELETE AIRTIMES FROM EXISTING GRID ENTRIES+++++
					//+++++++++++++++++++++++++++++++++++++++++++++++++++++


		$updateGrid[] = array($hour,$minute,$seconds,$ampm,$military,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$result[0][military]);
		}

	++$i;
	}
			//Created a 30 second spot from grid planner
			 if($_POST['fromPlanner'] == 'true'){
				$sql = "INSERT INTO $airtimes_Table values ('0','$hour','$minute','$seconds','$ampm','$military','$mon','$tue','$wed','$thu','$fri','$sat','$sun')";
				$result = $db->query($sql);
				if (DB::isError($result)) {
					die ($result->getMessage());
				}
			}else{
				 $sth = $db->prepare("UPDATE $airtimes_Table SET hour = ?,minute = ?,seconds = ?,ampm = ?,military = ?,mon = ?, tue = ?, wed = ?, thu = ?, fri = ?, sat = ?, sun = ? WHERE ID = ?");
				$result = $db->executeMultiple($sth, $alldata);
				 if (DB::isError($result)) {
				 die ($result->getMessage());
				 }






				 //update grid entries
				 for ($i = 0; $i < count($updateGrid); $i++) {

		 		$result = array();
				$sth = "SELECT * FROM $grid_Table WHERE military = '". $updateGrid[$i][12] ."'";
				$result = $db->getAll($sth,DB_FETCHMODE_ASSOC);
				if (DB::isError($result)) {
					die ($result->getMessage());
				}

					//echo count($result) . "<P>";
					//+++++++++++++++++++++++++++++++++++++
					//BEGIN SUB LOOP TO UPDATE GRID ENTRIES
					if(count($result) > 0){
						//echo "<P> Monday = " . $result[$i][mon];
							for ($m = 0; $m < count($result); $m++) {

						//echo $updateGrid[$i][5] . " - " .$result[$m][mon] . "<P>";
								//figure out the days to change in grid according to days droped from the air time
								if($updateGrid[$i][5] != 'true' && $result[$m][mon] == 'true'){//monday
									$gridMon = 'false';
								}else{
									$gridMon = $result[$m][mon];
								}

								if($updateGrid[$i][6] != 'true' && $result[$m][tue] == 'true'){//tuesday
									$gridTue = 'false';
								}else{
									$gridTue = $result[$m][tue];
								}

								if($updateGrid[$i][7] != 'true' && $result[$m][wed] == 'true'){//wednesday
									$gridWed = 'false';
								}else{
									$gridWed = $result[$m][wed];
								}

								if($updateGrid[$i][8] != 'true' && $result[$m][thu] == 'true'){//thursday
									$gridThu = 'false';
								}else{
									$gridThu = $result[$m][thu];
								}

								if($updateGrid[$i][9] != 'true' && $result[$m][fri] == 'true'){//friday
									$gridFri = 'false';
								}else{
									$gridFri = $result[$m][fri];
								}

								if($updateGrid[$i][10] != 'true' && $result[$m][sat] == 'true'){//saturday
									$gridSat = 'false';
								}else{
									$gridSat = $result[$m][sat];
								}

								if($updateGrid[$i][11] != 'true' && $result[$m][sun] == 'true'){//sunday
									$gridSun = 'false';
								}else{
									$gridSun = $result[$m][sun];
								}


								if($gridMon == 'false' && $gridTue == 'false' && $gridWed == 'false' && $gridThu == 'false' && $gridFri == 'false' && $gridSat == 'false' && $gridSun == 'false'){
									$deleteEntry = 'true';
								}else{
									$deleteEntry = 'false';
								}


							$updateGridFinal[] = array($updateGrid[$i][0],$updateGrid[$i][1],$updateGrid[$i][2],$updateGrid[$i][3],$updateGrid[$i][4],$gridMon,$gridTue,$gridWed,$gridThu,$gridFri,$gridSat,$gridSun,$result[$m][ID]);
							$deleteGridFinal[]=array($result[$m][ID],$deleteEntry);
//echo $updateGrid[$i][4] . " - " . $result[$i][spotID] . " - " . $gridFri . "<P>";



							}//end loop
					}//end if
					//END SUB LOOP TO UPDATE GRID ENTRIES
					//++++++++++++++++++++++++++++++++++++

				}//end loop


					//print_r($updateGridFinal);


				 //execute query to update grid
				 if(count($updateGridFinal) > 0){
					 $sth = $db->prepare("UPDATE $grid_Table SET hour = ?,minute = ?,seconds = ?,ampm = ?,military = ?,mon = ?, tue = ?, wed = ?, thu = ?, fri = ?, sat = ?, sun = ? WHERE ID = ?");
					 $result = $db->executeMultiple($sth, $updateGridFinal);
					 if (DB::isError($result)) {
					die ($result->getMessage());
					}
				}

				 $clearUnused = array();
				 //remove entries where all days are set to false
				 for ($i = 0; $i < count($deleteGridFinal); $i++) {
				 	if($deleteGridFinal[$i][1] == 'true'){
						$clearUnused[] = array($deleteGridFinal[$i][0]);
					}

				}

				//Execute  clear unused grid items
				if(count($clearUnused) > 0){
					 $sth = $db->prepare("DELETE FROM $grid_Table WHERE ID = ?");
					  $result = $db->executeMultiple($sth, $clearUnused);
					 if (DB::isError($result)) {
					 die ($result->getMessage());
					 }
				}


			}//end else stmt



	$_SESSION['message'] = "The selected air time(s) have been updated!";
	if($_POST['fromPlanner'] == 'true'){
		header("Location:index.php?ID=0&subMenu=2&action=planner");
	}else{
		header("Location:index.php?ID=0&subMenu=1&action=list");
	}

}
//++++++++++++++++++++++++++++
//END UPDATE AIRTIMES IN DB
//+++++++++++++++++++++++++++

	//#####################################################################

//+++++++++++++++++++++++++
//BEGIN MODIFY AIRTIMES BLOCK
//+++++++++++++++++++++++++
	if($action == 'airtime'){
		$_SESSION['updateAirTimeArray'] = array();
		$alldata = array();
		$GridAirtimeRemoval = array();


          $i=0;
				while($i < $_POST['highnumber']){
				$processID = $_POST["radioGroup$i"];

					if($processID){

					$action = substr("$processID", 0, (strpos($processID, ':')));
					$actionID = substr("$processID", ((strpos($processID, ':'))+1), strlen ("$processID"));
						//Delete Selected entries+++++


						if($action == 'delete'){
						$deleteEntry = 'true';
						$alldata[] = array($actionID);

					//+++++++++++++++++++++++++++++++++++++++++++++++++++++
					//BEGIN DELETE AIRTIMES FROM EXISTING GRID ENTRIES+++++
					//+++++++++++++++++++++++++++++++++++++++++++++++++++++
							$result = array();
							$sth = "SELECT military FROM $airtimes_Table WHERE ID = '$actionID'";
							$result = $db->getAll($sth,DB_FETCHMODE_ASSOC);
							if (DB::isError($result)) {
							die ($result->getMessage());
							}

							$GridAirtimeRemoval[] = $result[0][military];

					//+++++++++++++++++++++++++++++++++++++++++++++++++++++
					//BEGIN DELETE AIRTIMES FROM EXISTING GRID ENTRIES+++++
					//+++++++++++++++++++++++++++++++++++++++++++++++++++++
						}else{
						$_SESSION['updateAirTimeArray'][] = $actionID;
						}




				}
				++$i;
				}//end loop

				//BEGIN EXECUTE THE DELETE FROM GRID ENTRIES
				$sth = $db->prepare("DELETE from $grid_Table WHERE military = ?");
				$db->executeMultiple($sth, $GridAirtimeRemoval);
				//END EXECUTE THE DELETE FROM GRID ENTRIES

				$sth = $db->prepare("DELETE from $airtimes_Table WHERE ID = ?");
				$db->executeMultiple($sth, $alldata);

				if($deleteEntry != 'true' && count($_SESSION['updateAirTimeArray']) < 1){//no items were selected to update or delete
				$_SESSION['message'] = "NO ACTION TAKEN.  Please select an Air Time to update or delete!";
				header("Location:index.php?ID=0&subMenu=1");
				}else if($deleteEntry == 'true' && count($_SESSION['updateAirTimeArray']) < 1){//items to delete but not update
				$_SESSION['message'] = "The selected air time(s) have been removed!";
				header("Location:index.php?ID=0&subMenu=1");
				}else if($deleteEntry == 'true' && count($_SESSION['updateAirTimeArray']) > 0){//items to delete and update
				$_SESSION['message'] = "The selected air time(s) have been removed. Here are the air time(s) to update!";
				header("Location:index.php?ID=0&subMenu=1&action=update");
				}else if($deleteEntry != 'true' && count($_SESSION['updateAirTimeArray']) > 0){//items just to update
				$_SESSION['message'] = "Here are the air time(s) to update!";
				header("Location:index.php?ID=0&subMenu=1&action=update");
				}

	}
			//+++++++++++++++++++++++++
			//END MODIFY AIRTIMES BLOCK
			//+++++++++++++++++++++++++



			//+++++++++++++++++++++++++
			//BEGIN ENTER AIRTIMES BLOCK
			//+++++++++++++++++++++++++
			if($action == 'enterairtime'){
				$highnumber = $_POST['highnumber'];
				$hour = ereg_replace ('[^0-9]+', '', $_POST['hour']);
				$minute = ereg_replace ('[^0-9]+', '', $_POST['minute']);
				$seconds = ereg_replace ('[^0-9]+', '', $_POST['seconds']);
				$ampm = $_POST['ampm'];

				if($hour == "" || $minute == ""){
				$validTime = 'false';
				}

				calcMilitary($hour,$minute,$seconds,$ampm);


				if(!$_POST['mon'] && !$_POST['tue'] && !$_POST['wed'] && !$_POST['thu'] && !$_POST['fri'] && !$_POST['sat'] && !$_POST['sun']){
				$daysSelected = 'false';
				}else{
				if(!$_POST['mon']){
				$mon = 'false';
				}else{
				$mon = $_POST['mon'];
				}

				if(!$_POST['tue']){
				$tue = 'false';
				}else{
				$tue = $_POST['tue'];
				}

				if(!$_POST['wed']){
				$wed = 'false';
				}else{
				$wed = $_POST['wed'];
				}

				if(!$_POST['thu']){
				$thu = 'false';
				}else{
				$thu = $_POST['thu'];
				}

				if(!$_POST['fri']){
				$fri = 'false';
				}else{
				$fri = $_POST['fri'];
				}

				if(!$_POST['sat']){
				$sat = 'false';
				}else{
				$sat = $_POST['sat'];
				}

				if(!$_POST['sun']){
				$sun = 'false';
				}else{
				$sun = $_POST['sun'];
				}


				$sql = "SELECT * FROM $airtimes_Table WHERE  military = '$military'";
				$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
				 if (DB::isError($result)) {
				 die ($result->getMessage());
				 }

				$num = count($result);
				if($num == '0'){
					if($hour != "" && $minute != ""){
					$newEntry = 'true';

				$sql = "INSERT into $airtimes_Table values ('0','$hour','$minute','$seconds','$ampm','$military','$mon','$tue','$wed','$thu','$fri','$sat','$sun')";
				$result = $db->query($sql);
				if (DB::isError($result)) {
				die ($result->getMessage());
				}
					}
				}else{
					$timeExists = 'true';
				}



				}//end if no days selected


				if($newEntry == 'true'){
				$_SESSION['message'] = "The new air time $hour:$minute:$seconds $ampm has been added!";
				}else if($timeExists == 'true'){
				$_SESSION['message'] = "$hour:$minute:$seconds $ampm already exists in the database.  Please choose another Time!";
				}else if($daysSelected == 'false'){
				$_SESSION['message'] = "AIR TIME NOT ENTERED.  Please select days of week before submitting.";
				}else if($validTime == 'false'){
				$_SESSION['message'] = "AIR TIME NOT ENTERED.  Please select valid hour and minute before submitting.";
				}

					if($_POST['fromPlanner'] == 'true'){
					  header("Location:index.php?ID=0&subMenu=2&action=planner");
					}else{
				      header("Location:index.php?ID=0&subMenu=1&action=enter");
					}

			}
			//+++++++++++++++++++++++++
			//END ENTER AIRTIMES BLOCK
			//+++++++++++++++++++++++++

	//##############################################################################################################
	//BEGIN MANAGE GRID CODE BLOCKS
	//##############################################################################################################

			//++++++++++++++++++++++++++++++++++++++++
			//BEGIN ADD FIELD FROM ENTRY FORM
			//++++++++++++++++++++++++++++++++++++++++



//########################################################################################################

			//++++++++++++++++++++++++++++++++++++++++
			//BEGIN GENERATE CONFIRM SUBMISSION ARRAYS
			//++++++++++++++++++++++++++++++++++++++++
			$mulitpleHighNumber = $_POST['mulitpleHighNumber'];

			if($_POST['myAction'] == 'generateVerification'){

				$_SESSION['name'] = $_POST['name'];
				//GET REAL UNDERWRITER NAME
				$abbreviation = $_SESSION['name'];
				$sql = "SELECT name FROM $underwriter_Table WHERE abbreviation = '$abbreviation'";
				$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
					if (DB::isError($result)) {
					die ($result->getMessage());
					}
 			   	//$underwriter = $result[0][name];
				$_SESSION['keepUW'] = $result[0][name];
				$_SESSION['startMonth'] = $_POST['startMonth'];
				$_SESSION['startDay'] = $_POST['startDay'];
				$_SESSION['startYear'] = $_POST['startYear'];
				$_SESSION['endMonth'] = $_POST['endMonth'];
				$_SESSION['endDay'] = $_POST['endDay'];
				$_SESSION['endYear'] = $_POST['endYear'];
				$_SESSION['length'] = ereg_replace ('[^0-9]+', '', $_POST['length']);
				$_SESSION['spotID'] = $_POST['spotID'];
				$_SESSION['type'] = $_POST['type'];
				$_SESSION['AccurateTimeStamps'] = array();
				$_SESSION['verifyTimeArray'][] = array();
				$_SESSION['verifySecondsArray'][] = array();
				$_SESSION['verifyMondayArray'][] = array();
				$_SESSION['verifyTuesdayArray'][] = array();
				$_SESSION['verifyWednesdayArray'][] = array();
				$_SESSION['verifyThursdayArray'][] = array();
				$_SESSION['verifyFridayArray'][] = array();
				$_SESSION['verifySaturdayArray'][] = array();
				$_SESSION['verifySundayArray'][] = array();
				$_SESSION['verifyTypeArray'][] = array();
				$_SESSION['verifyRateArray'][] = array();
				$_SESSION['verifyLiveArray'][] = array();
				$_SESSION['verifyTradeArray'][] = array();

     			$i = 0;
				$q = 1;
				while ($i < $_POST['mulitpleHighNumber']){
					$_SESSION['verifyTimeArray'][$i] = $_POST["time$q"];
					$_SESSION['verifySecondsArray'][$i] = $_POST["seconds$q"];
					$_SESSION['verifyMondayArray'][$i] = $_POST["mon$q"];
					$_SESSION['verifyTuesdayArray'][$i] = $_POST["tue$q"];
					$_SESSION['verifyWednesdayArray'][$i] = $_POST["wed$q"];
					$_SESSION['verifyThursdayArray'][$i] = $_POST["thu$q"];
					$_SESSION['verifyFridayArray'][$i] = $_POST["fri$q"];
					$_SESSION['verifySaturdayArray'][$i] = $_POST["sat$q"];
					$_SESSION['verifySundayArray'][$i] = $_POST["sun$q"];
					$_SESSION['verifyRateArray'][$i] = ereg_replace ('[^0-9.]+', '', $_POST["rate$q"]);
					$_SESSION['verifyLiveArray'][$i] = $_POST["live$q"];
					$_SESSION['verifyTradeArray'][$i] = $_POST["trade$q"];
					//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
					//BEGIN section to update info in $_SESSION['plannerEntry'] so it can be remembered
					//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
					$tempDaysArray = array();
					if($_SESSION['verifyMondayArray'][$i] == 'true'){
					array_push ($tempDaysArray, "mon");
					}
					if($_SESSION['verifyTuesdayArray'][$i] == 'true'){
					array_push ($tempDaysArray, "tue");
					}
					if($_SESSION['verifyWednesdayArray'][$i] == 'true'){
					array_push ($tempDaysArray, "wed");
					}
					if($_SESSION['verifyThursdayArray'][$i] == 'true'){
					array_push ($tempDaysArray, "thu");
					}
					if($_SESSION['verifyFridayArray'][$i] == 'true'){
					array_push ($tempDaysArray, "fri");
					}
					if($_SESSION['verifySaturdayArray'][$i] == 'true'){
					array_push ($tempDaysArray, "sat");
					}
					if($_SESSION['verifySundayArray'][$i] == 'true'){
					array_push ($tempDaysArray, "sun");
					}

					//$_SESSION['plannerEntry'][$_POST["rememberTime$q"]][military] = substr($_SESSION['verifyTimeArray'][$i], 0, 6) . $_SESSION['verifySecondsArray'][$i];   HANDLED DOWN BELOW SO AS TO GET PROPER MILITARY TIME
					$_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days] = array_unique ($tempDaysArray);
					$_SESSION['plannerEntry'][$_POST["rememberTime$q"]][rate] = $_SESSION['verifyRateArray'][$i];
					$_SESSION['plannerEntry'][$_POST["rememberTime$q"]][live] = $_SESSION['verifyLiveArray'][$i];
					$_SESSION['plannerEntry'][$_POST["rememberTime$q"]][trade] = $_SESSION['verifyTradeArray'][$i];
					//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
					//END section to update info in $_SESSION['plannerEntry'] so it can be remembered
					//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				++$i;
				++$q;

				}




			$_SESSION['mulitpleHighNumber'] = $_POST['mulitpleHighNumber'];

		//#################################################
		//BEGIN LOOK FOR SCHEDULING CONFLICTS
		//#################################################
		$startMonth = $_SESSION['startMonth'];
		$startDay = $_SESSION['startDay'];
		$startYear = $_SESSION['startYear'];
		$endMonth = $_SESSION['endMonth'];
		$endDay = $_SESSION['endDay'];
		$endYear = $_SESSION['endYear'];
		$startDateStamp = mktime(0,0,0,$startMonth,$startDay,$startYear);
		$_SESSION['rememberStartStamp'] = $startDateStamp;//used to remember info when bouncing from enter grid to planner, etc.
		if($endMonth != 'NA' && $endDay != 'NA' && endYear != 'NA'){
		$endDateStamp = mktime(0,0,0,$endMonth,$endDay,$endYear);
		}else{
		$endDateStamp = 'NA';
		}







			$_SESSION['scheduleConflictArray'] = array();
			$_SESSION['displayConflicts'] = array();

		//Check to make sure end date, if set, is later than start date
		if($endDateStamp != 'NA' && $endDateStamp < $startDateStamp){
			$_SESSION['scheduleConflictArray'][] = "<B> WARNING: </B>The end date must be after the start date!";
		}

							$i = 0;
							$q = 1;
							while($i < $_SESSION['mulitpleHighNumber']){

		//Global Functions - Determine stamps down to the day
		$firstDayID = array_search((reset( $_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days])),$daysOfWeek);
		$lastDayID = array_search((end( $_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days])),$daysOfWeek);
		$FormStartDayID = array_search((strtolower(date("D",intval($startDateStamp)))),$daysOfWeek);
		$FormEndDayID = array_search((strtolower(date("D",intval($endDateStamp)))),$daysOfWeek);
		$NumberOfDaysUsed = count($_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days]);
		if($NumberOfDaysUsed > 1){
			for ($x = 0; $x < $NumberOfDaysUsed; $x++) {
				if(  array_search(($_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days][$x]),$daysOfWeek)  > $FormEndDayID){
					$NextLowestDay = array_search(($_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days][$x-1]),$daysOfWeek);
					break;
				}
			}

			for ($x = 0; $x < $NumberOfDaysUsed; $x++) {
				if(  array_search(($_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days][$x]),$daysOfWeek)  > $FormStartDayID){
					$NextHighestDay = array_search(($_SESSION['plannerEntry'][$_POST["rememberTime$q"]][days][$x]),$daysOfWeek);
					break;
				}
			}
		}
		DetermineAccurateTimeStamps($startDateStamp,$endDateStamp,$firstDayID,$lastDayID,$NextHighestDay,$NextLowestDay,$NumberOfDaysUsed,$FormStartDayID,$FormEndDayID);
		$_SESSION['AccurateTimeStamps'][$i][startTimeStamp] = $_SESSION['accurateStartTime'];//For individual entries in grid planner
		$_SESSION['AccurateTimeStamps'][$i][endTimeStamp] = $_SESSION['accurateEndTime'];//These aren't used until actually enter grid items into DB

						$time = $_SESSION['verifyTimeArray']["$i"];
						$hour = substr($time, 0, 2);
						$minute = substr($time, 3, 2);
						$seconds = $_SESSION['verifySecondsArray']["$i"];
						$ampm = substr($time, 6, 2);

							//DETERMINE MILITARY TIME
							calcMilitary($hour,$minute,$seconds,$ampm);

					$_SESSION['plannerEntry'][$_POST["rememberTime$q"]][military] = $military;// add to planner array
							$mon = $_POST["mon$q"];
						    $tue = $_POST["tue$q"];
							$wed = $_POST["wed$q"];
							$thu = $_POST["thu$q"];
							$fri = $_POST["fri$q"];
							$sat = $_POST["sat$q"];
							$sun = $_POST["sun$q"];

			$sql = "SELECT * FROM $grid_Table WHERE military = '$military' ORDER BY military";
			$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
			 if (DB::isError($result)) {
			 die ($result->getMessage());
			 }
			$num = count($result);

			if($num > '0'){

				for ($c = 0; $c < count($result); $c++) {

					$myunderwriter = $result[$c][underwriter];
					$myStartTime = $result[$c][startTimeStamp];
					$myEndTime = $result[$c][endTimeStamp];
					$myhour = $result[$c][hour];
					$myminute = $result[$c][minute];
					$myseconds = $result[$c][seconds];
					$myampm = $result[$c][ampm];
					$mymon = $result[$c][mon];
					$mytue = $result[$c][tue];
					$mywed = $result[$c][wed];
					$mythu = $result[$c][thu];
					$myfri = $result[$c][fri];
					$mysat = $result[$c][sat];
					$mysun = $result[$c][sun];
					$mySpotID = $result[$c][spotID];

						$conflictDays = array();

						if($_SESSION['accurateStartTime'] > $myEndTime && $myEndTime != 'NA'){//New Entry is all after the DB entry - ALL CLEAR
							$checkDays = 'false';
						}else if($_SESSION['accurateEndTime'] < $myStartTime && $endDateStamp != 'NA'){//New Entry is all B4 the DB entry - ALL CLEAR
							$checkDays = 'false';
						}else{
							$checkDays = 'true';//There is overlap
						}

				//GO through each Day to see if any overlap
				if($checkDays == 'true'){
					if($mymon == 'true' && $mon == 'true'){
					$conflictDays[] = "mon";
					}
					if($mytue == 'true' && $tue == 'true'){
					$conflictDays[] = "tue";
					}
					if($mywed == 'true' && $wed == 'true') {
					$conflictDays[] = "wed";
					}
					if($mythu == 'true' && $thu == 'true'){
					$conflictDays[] = "thu";
					}
					if($myfri == 'true' && $fri == 'true'){
					$conflictDays[] = "fri";
					}
					if($mysat == 'true' && $sat == 'true'){
					$conflictDays[] = "sat";
					}
					if($mysun == 'true' && $sun == 'true'){
					$conflictDays[] = "sun";
					}
				}//end if check days = true








				//found something
				if(count($conflictDays) > '0'){
						$z = 0;
						while($z < count($conflictDays)){
						$myDisplayDays = $myDisplayDays . " " . $conflictDays[$z];//put all conflicting days in a string
						++$z;
						}

						if($myEndTime != 'NA'){
						$dsiplayEndTime = date("n/j/Y",$myEndTime);
						}else{
						$dsiplayEndTime = 'NA';
						}
						$_SESSION['scheduleConflictArray'][] = "<BR>&nbsp;&#8226;". $_SESSION['spotID'] ." Conflicts With: $mySpotID | $myhour:$myminute:$myseconds $myampm | Starting: " . date("n/j/y",$myStartTime) . " | Ending: $dsiplayEndTime | $myDisplayDays";
						$_SESSION['displayConflicts'][$i] = "$military:$ampm";
						$myDisplayDays = "";

				}
			}//end result loop



			}// end if($num > '0'){
			++$i;
			++$q;
			}//END MAIN LOOP







			$_SESSION['displayConflicts'] = array_unique($_SESSION['displayConflicts']); // get rid of dulpicates
			$myMessage = "";

			if(count($_SESSION['scheduleConflictArray']) > 0){ //Creat string for session message showing alconflicting entries
				$L = 0;
				while($L < count($_SESSION['scheduleConflictArray'])){
				$myMessage = $myMessage . " " . $_SESSION['scheduleConflictArray'][$L];
				++$L;
				}
			$_SESSION['message'] = $myMessage;
			$_SESSION['gridDuplicate'] = 'true';
			}else{
				$_SESSION['message'] = "Please double check your info before submitting!";
			}
			$_SESSION['PlannerStamp'] = $_POST['PlannerStamp'];


			//++++++++++++++++++++++++++++++++++++++++++
			//BEGIN SORT PLANNER ARRAY BY MILITARY TIMES
			//++++++++++++++++++++++++++++++++++++++++++
				   //sort array in military order
				   if(!$_SESSION['plannerEntry']){
				   $_SESSION['plannerEntry'] = array();
				   }
				   $my_array = multi_sort($_SESSION['plannerEntry'], "military", true);
					$_SESSION['plannerEntry'] = $my_array;
			//++++++++++++++++++++++++++++++++++++++++++
			//END SORT PLANNER ARRAY BY MILITARY TIMES
			//++++++++++++++++++++++++++++++++++++++++++

			if(isset($_GET['generateMultiples'])){
				$newArrayItem  = array();
				$newArrayItem[military] =  "00:00:00";
				$newArrayItem[days][0] =  "null";
				array_unshift ($_SESSION['plannerEntry'], $newArrayItem);//add blank item to front of array
				unset($_SESSION['message']);//no need for messages yet
				unset($_SESSION['displayConflicts']);
				header("Location:index.php?ID=0&subMenu=2&action=enter");
			}else if(isset($_GET['rememberInfo'])){
				unset($_SESSION['message']);//no need for messages yet
				unset($_SESSION['displayConflicts']);
				header("Location:index.php?ID=0&subMenu=2&action=enter");//Just send back to entry form after loading up planner array
			}else if(isset($_GET['getSpotIDs'])){//get all spot IDs to list out
				unset($_SESSION['displayConflicts']);
				unset($_SESSION['message']);//no need for messages yet
			$sql = "SELECT spotID FROM $grid_Table WHERE underwriter = '".mysql_real_escape_string (mysql_real_escape_string ($_SESSION['keepUW']))."'";
			$_SESSION['viewSpots'] = $db->getAll($sql, DB_FETCHMODE_ASSOC);
				 if (DB::isError($result)) {
				 die ($result->getMessage());
				 }

			$tempArray = array();
			for ($i = 0; $i < count($_SESSION['viewSpots']); $i++) {
				$tempArray[] = $_SESSION['viewSpots'][$i][spotID];
			}


 			$tempArray = array_unique($tempArray);


			$_SESSION['viewSpots'] = $tempArray;
			//print_r($_SESSION['viewSpots']);
			//echo "<P>&nbsp;</P>";
			//print_r($tempArray);
				header("Location:index.php?ID=0&subMenu=2&action=enter");
			}else{
			//++++++++++++++++++++++++++++++++++++++++
			//END ADD FIELD FROM ENTRY FORM
			//++++++++++++++++++++++++++++++++++++++++
				header("Location:index.php?ID=0&subMenu=2&action=verify");
			}

//echo $_SESSION['message'];
		//#################################################
		//END LOOK FOR SCHEDULING CONFLICTS
		//#################################################


			}
			//++++++++++++++++++++++++++++++++++++++++
			//END GENERATE CONFIRM SUBMISSION ARRAYS
			//++++++++++++++++++++++++++++++++++++++++


	//*********************************************************************************************************************************
				//##################################
				//BEGIN CODE TO UPDATE OLD ENTRIES--
				//##################################

				if($action == 'updateOldGridItems'){
					$_SESSION['updateArray'] = array();
					$_SESSION['scheduleConflictArray'] = array();
					$updatedSpots = array();
					$alldata = array();//for PEAR execute multiple

					$i=0;
					while($i < $mulitpleHighNumber){
					$ID = $_POST["upDateID$i"];
					if($ID){
					$addData = 'true';
					//FOUND ITEM TO UPDATE - RUN IT THROUGH ITS PACES

		//#################################################
		//BEGIN LOOK FOR SCHEDULING CONFLICTS
		//#################################################
	$abbreviation = $_POST["name$i"];
	$Freshresult = $db->getAll("SELECT name FROM $underwriter_Table WHERE abbreviation = '$abbreviation'", DB_FETCHMODE_ASSOC);
     if (DB::isError($Freshresult)) {
     die ($Freshresult->getMessage());
     }
	$underwriter = 	$Freshresult[0][name];
		$length = $_POST["length$i"];
		$spotID = $_POST["spotID$i"];
		$type = $_POST["type$i"];
		$rate = $_POST["rate$i"];
		$trade = $_POST["trade$i"];
		$live = $_POST["live$i"];
		$mon = $_POST["mon$i"];
		$tue = $_POST["tue$i"];
		$wed = $_POST["wed$i"];
		$thu = $_POST["thu$i"];
		$fri = $_POST["fri$i"];
		$sat = $_POST["sat$i"];
		$sun = $_POST["sun$i"];
		$startMonth = $_POST["startMonth$i"];
		$startDay = $_POST["startDay$i"];
		$startYear = $_POST["startYear$i"];
		$endMonth = $_POST["endMonth$i"];
		$endDay = $_POST["endDay$i"];
		$endYear = $_POST["endYear$i"];
		$startDateStamp = mktime(0,0,0,$startMonth,$startDay,$startYear);
		if($endMonth != 'NA' && $endDay != 'NA' && endYear != 'NA'){
		$endDateStamp = mktime(0,0,0,$endMonth,$endDay,$endYear);
		}else{
		$endDateStamp = 'NA';
		}

		//Check to make sure end date, if set, is later than start date
		if($endDateStamp != 'NA' && $endDateStamp < $startDateStamp){
			$_SESSION['updateArray'][] = $ID;
			$_SESSION['scheduleConflictArray'][] = "<BR>&nbsp;&#8226; <B>WARNING: </B>The end date must be after the start date for $spotID!";
		}

		$DaysForEachEntry = array();

			if ($mon == 'true'){
				array_push($DaysForEachEntry,"mon");
			}
			if ($tue == 'true'){
				array_push($DaysForEachEntry,"tue");
			}
			if ($wed == 'true'){
				array_push($DaysForEachEntry,"wed");
			}
			if ($thu == 'true'){
				array_push($DaysForEachEntry,"thu");
			}
			if ($fri == 'true'){
				array_push($DaysForEachEntry,"fri");
			}
			if ($sat == 'true'){
				array_push($DaysForEachEntry,"sat");
			}
			if ($sun == 'true'){
				array_push($DaysForEachEntry,"sun");
			}


		//Determine accurate start and end times down to the DAY
		$firstDayID = array_search((reset($DaysForEachEntry)),$daysOfWeek);
		$lastDayID = array_search((end($DaysForEachEntry)),$daysOfWeek);
		$FormStartDayID = array_search((strtolower(date("D",$startDateStamp))),$daysOfWeek);
		$FormEndDayID = array_search((strtolower(date("D",$endDateStamp))),$daysOfWeek);
		$NumberOfDaysUsed = count($DaysForEachEntry);
		if($NumberOfDaysUsed > 1){
			for ($x = 0; $x < $NumberOfDaysUsed; $x++) {
				if(  array_search(($DaysForEachEntry[$x]),$daysOfWeek)  > $FormEndDayID){
					$NextLowestDay = array_search(($DaysForEachEntry[$x-1]),$daysOfWeek);
					break;
				}
			}

			for ($x = 0; $x < $NumberOfDaysUsed; $x++) {
				if(  array_search(($DaysForEachEntry[$x]),$daysOfWeek)  > $FormStartDayID){
					$NextHighestDay = array_search(($DaysForEachEntry[$x]),$daysOfWeek);
					break;
				}
			}
		}
		DetermineAccurateTimeStamps($startDateStamp,$endDateStamp,$firstDayID,$lastDayID,$NextHighestDay,$NextLowestDay,$NumberOfDaysUsed,$FormStartDayID,$FormEndDayID);


		$time = $_POST["time$i"];
		$hour = substr($time, 0, 2);
		$minute = substr($time, 3, 2);
		$seconds = $_POST["seconds$i"];
		$ampm = substr($time, 6, 2);

		//DETERMINE MILITARY TIME
		calcMilitary($hour,$minute,$seconds,$ampm);

	$sql = "SELECT * FROM $grid_Table WHERE military = '$military' AND ID != '$ID' ORDER BY military";
	$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
     if (DB::isError($result)) {
     die ($result->getMessage());
     }
	$num = count($result);



	//CHECK TO SEE IF MILITARY TIME EXISTS IN AIR_TIMES TABLE
	$sqlA = "SELECT * FROM $airtimes_Table WHERE military = '$military'";
	$resultA = $db->getAll($sqlA, DB_FETCHMODE_ASSOC);
     if (DB::isError($resultA)) {
     die ($resultA->getMessage());
     }
	$numA = count($resultA);

	if($numA == '0'){//need to create a new entry in air times table so see if non half minute time exists
		$sqlB = "SELECT * FROM $airtimes_Table WHERE hour = '$hour' AND minute = '$minute' AND ampm = '$ampm'";
		$resultB = $db->getAll($sqlB, DB_FETCHMODE_ASSOC);
		 if (DB::isError($resultB)) {
		 die ($resultB->getMessage());
		 }
		$numB = count($resultB);


		if($numB > '0' && $seconds == '30'){
				$sqlC = "INSERT INTO $airtimes_Table values ('0','$hour','$minute','$seconds','$ampm','$military','" . $resultB[0][mon] . "','" . $resultB[0][tue] . "','" . $resultB[0][wed] . "','" . $resultB[0][thu] . "','" . $resultB[0][fri] . "','" . $resultB[0][sat] . "','" . $resultB[0][sun] . "')";
				$resultC = $db->query($sqlC);
				if (DB::isError($resultC)) {
					die ($resultC->getMessage());
				}

		}

	}




			if($num > 0){//There were DB entries with the same military time as what's coming from the form
					for ($n = 0; $n < $num; $n++) {
					$myID = $result[$n][ID];
					$myunderwriter = $result[$n][underwriter];
					$myStartTime = $result[$n][startTimeStamp];
					$myEndTime = $result[$n][endTimeStamp];
					$myhour = $result[$n][hour];
					$myminute = $result[$n][minute];
					$myseconds = $result[$n][seconds];
					$myampm = $result[$n][ampm];
					$mymon = $result[$n][mon];
					$mytue = $result[$n][tue];
					$mywed = $result[$n][wed];
					$mythu = $result[$n][thu];
					$myfri = $result[$n][fri];
					$mysat = $result[$n][sat];
					$mysun = $result[$n][sun];
					$mySpotID = $result[$n][spotID];

						$conflictDays = array();
						if($_SESSION['accurateStartTime'] > $myEndTime && $myEndTime != 'NA'){//New Entry is all after the DB entry - ALL CLEAR
							$checkDays = 'false';
						}else if($_SESSION['accurateEndTime'] < $myStartTime && $_SESSION['accurateEndTime'] != 'NA'){//New Entry is all B4 the DB entry - ALL CLEAR
							$checkDays = 'false';
						}else{
							$checkDays = 'true';//There is overlap
						}


				//GO through each Day to see if any overlap
				if($checkDays == 'true'){
					if($mymon == 'true' && $mon == 'true'){
					$conflictDays[] = "mon";
					}
					if($mytue == 'true' && $tue == 'true'){
					$conflictDays[] = "tue";
					}
					if($mywed == 'true' && $wed == 'true') {
					$conflictDays[] = "wed";
					}
					if($mythu == 'true' && $thu == 'true'){
					$conflictDays[] = "thu";
					}
					if($myfri == 'true' && $fri == 'true'){
					$conflictDays[] = "fri";
					}
					if($mysat == 'true' && $sat == 'true'){
					$conflictDays[] = "sat";
					}
					if($mysun == 'true' && $sun == 'true'){
					$conflictDays[] = "sun";
					}
				}//end if check days = true

					if(count($conflictDays) != 0){ //we have some overlap
						$z = 0;
						while($z < count($conflictDays)){
						$myDisplayDays = $myDisplayDays . " " . $conflictDays[$z];
						++$z;
						}

						if($myEndTime != 'NA'){
						$dsiplayEndTime = date("n/j/Y",$myEndTime);
						}else{
						$dsiplayEndTime = 'NA';
						}
						$_SESSION['scheduleConflictArray'][] = "<BR>&nbsp;&#8226; $spotID Conflicts With: $mySpotID | Starting: " . date("n/j/y",$myStartTime) . " | Ending: $dsiplayEndTime | $myDisplayDays";
						$myDisplayDays = "";

						$addData = 'false';

					}



			}//ENd FOR LOOP - Fetch Mysql results loop



					if($addData == 'true'){//none of the days were in conflict
							if($mon != 'true' || !$mon){
							$mon = 'false';
							}
							if($tue != 'true' || !$tue){
							$tue = 'false';
							}
							if($wed != 'true' || !$wed){
							$wed = 'false';
							}
							if($thu != 'true' || !$thu){
							$thu = 'false';
							}
							if($fri != 'true' || !$fri){
							$fri = 'false';
							}
							if($sat != 'true' || !$sat){
							$sat = 'false';
							}
							if($sun != 'true' || !$sun){
							$sun = 'false';
							}

							$rate = ereg_replace ('[^0-9.]+', '', $rate);

							if(!$live || $live == ""){
							$live = 'false';
							}
							if(!$trade){
							$trade = 'false';
							}

							$underwriter = mysql_real_escape_string ($underwriter);
							$abbreviation = mysql_real_escape_string ($abbreviation);

							if(array_search ($ID,$_SESSION['updateArray']) === false){//make sure ID was not added due to date check to see if start is after end date
								//$alldata[] = array($underwriter,$abbreviation,$_SESSION['accurateStartTime'],$_SESSION['accurateEndTime'],$length,$spotID,$hour,$minute,$seconds,$ampm,$military,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$type,$rate,$live,$trade,$ID);
								$alldata[] = array(0,$underwriter,$abbreviation,$_SESSION['accurateStartTime'],$_SESSION['accurateEndTime'],$length,$spotID,$hour,$minute,$seconds,$ampm,$military,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$type,$rate,$live,$trade,$ID);
							}
							$updatedSpots[] = "<BR>&nbsp;&#8226; " . $spotID . " at $hour:$minute:$seconds $ampm";

				}else{
					$_SESSION['updateArray'][] = $ID;
				}



			//#############################################
		}else{//No Existing entries for Military time



							if($mon != 'true' || !$mon){
							$mon = 'false';
							}
							if($tue != 'true' || !$tue){
							$tue = 'false';
							}
							if($wed != 'true' || !$wed){
							$wed = 'false';
							}
							if($thu != 'true' || !$thu){
							$thu = 'false';
							}
							if($fri != 'true' || !$fri){
							$fri = 'false';
							}
							if($sat != 'true' || !$sat){
							$sat = 'false';
							}
							if($sun != 'true' || !$sun){
							$sun = 'false';
							}

							$rate = ereg_replace ('[^0-9.]+', '', $rate);

							if(!$live || $live == ""){
							$live = 'false';
							}
							if(!$trade){
							$trade = 'false';
							}

							$underwriter = mysql_real_escape_string ($underwriter);
							$abbreviation = mysql_real_escape_string ($abbreviation);
							if(array_search ($ID,$_SESSION['updateArray']) === false){//make sure ID was not added due to date check to see if start is after end date
								$alldata[] = array(0,$underwriter,$abbreviation,$_SESSION['accurateStartTime'],$_SESSION['accurateEndTime'],$length,$spotID,$hour,$minute,$seconds,$ampm,$military,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$type,$rate,$live,$trade,$ID);
							}
							$updatedSpots[] = "<BR>&nbsp;&#8226; " . $spotID . " at $hour:$minute:$seconds $ampm";
		}




		}//end if $ID







		++$i;
		}//end main while loop
		$_SESSION['updateArray'] = array_unique($_SESSION['updateArray']);//get rid of any duplicates from making sure end date is after start date


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//BEGIN Conjur up method to split spots apart if they have more than one day asigned to them
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$TEMP_alldata = array();
for ($i = 0; $i < count($alldata); $i++) {

		//Delete current entries and replace them with new entries where days are split up 23
		$sql = "DELETE FROM $grid_Table WHERE ID = '" .$alldata[$i][23] ."'";
		$result = $db->query($sql);
		if (DB::isError($result)) {
		die ($result->getMessage());
		}


		if($alldata[$i][12] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'true','false','false','false','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][13] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','true','false','false','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][14] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','true','false','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][15] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','true','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][16] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','false','true','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][17] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','false','false','true','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][18] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','false','false','false','true',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//END Conjur up method to split spots apart if they have more than one day asigned to them
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/* print_r($alldata);
echo "<P>&nbsp;</P>";
print_r($TEMP_alldata); */

if(count($TEMP_alldata) != 0){
	$sth = $db->prepare("INSERT INTO $grid_Table VALUES ('?',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    $db->executeMultiple($sth, $TEMP_alldata);
}
//DONE LOOPING PROCESS ARRAYS AND SEND TO APPROPRIATE PLACE


			if(count($_SESSION['updateArray']) == '0' && $_SESSION['backtogrid'] != 'true'){//send it to view live grid
				$z = 0;
				while($z < count($updatedSpots)){

				$myDisplaySpots = $myDisplaySpots . " " . $updatedSpots[$z];
				++$z;
				}
			    $_SESSION['timestamp'] = $_POST['maintainViewStamp'];
				$_SESSION['message'] = "<BR>The following spot(s) have been updated: $myDisplaySpots";
				header("Location:index.php?ID=0&subMenu=2&action=view");
			}else if(count($_SESSION['updateArray']) == '0' && $_SESSION['backtogrid'] == 'true'){//send it back to weekly planner
				unset($_SESSION['backtogrid']);
				$z = 0;
				while($z < count($updatedSpots)){
					$myDisplaySpots = $myDisplaySpots . " " . $updatedSpots[$z];
				++$z;
				}
			    $_SESSION['timestamp'] = $_POST['maintainViewStamp'];
				$_SESSION['message'] = "<BR>The following spot(s) have been updated: $myDisplaySpots";
				header("Location:index.php?ID=0&subMenu=2&action=planner");
			}else{
				$_SESSION['timestamp'] = $_POST['maintainViewStamp'];
				$L = 0;
				while($L < count($_SESSION['scheduleConflictArray'])){
				$myMessage = $myMessage . " " . $_SESSION['scheduleConflictArray']["$L"];
				++$L;
				}
			$_SESSION['message'] = $myMessage;
			header("Location:index.php?ID=0&subMenu=2&action=modifyGrid");
			}

				}//END if($action == 'updateOldGridItems'){
				//##################################
				//END CODE TO UPDATE OLD ENTRIES--
				//##################################

	//*********************************************************************************************************************************

			//+++++++++++++++++++++++++
			//BEGIN ENTER GRID ITEM
			//+++++++++++++++++++++++++
			if($action == 'enterGrid'){
			$highNumber = $_POST['highNumber'];
							$underwriter = mysql_real_escape_string ($_POST['underwriter']);
							$abbreviation = mysql_real_escape_string ($_POST['abbreviation']);
							/* $startTimeStamp = mktime(0,0,0,$_POST['startMonth'],$_POST['startDay'],$_POST['startYear']);
								if($_POST['endMonth'] == 'NA' || $_POST['endDay'] == 'NA' || $_POST['endYear'] == 'NA'){
								$endTimeStamp = 'NA';
								}else{
								$endTimeStamp = mktime(0,0,0,$_POST['endMonth'],$_POST['endDay'],$_POST['endYear']);
								} */
							$length = ereg_replace ('[^0-9]+', '', $_POST['length']);
							$spotID = mysql_real_escape_string ($_POST['spotID']);
							$type = $_POST['type'];

						$alldata = array();


							//LOOP
							$i = 0;
							$q = 1;
							while($i < $highNumber){

							$hour = $_POST["hour$q"];
							$minute = $_POST["minute$q"];
							$ampm = $_POST["ampm$q"];
							$seconds = $_POST["seconds$q"];
							//DETERMINE MILITARY TIME
							calcMilitary($hour,$minute,$seconds,$ampm);

	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//BEGIN CHECK TO SEE IF MILITARY TIME EXISTS IN AIR_TIMES TABLE
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	$sqlA = "SELECT * FROM $airtimes_Table WHERE military = '$military'";
	$resultA = $db->getAll($sqlA, DB_FETCHMODE_ASSOC);
     if (DB::isError($resultA)) {
     die ($resultA->getMessage());
     }
	$numA = count($resultA);
	//echo "Previous Entries: $numA<P>";

	if($numA == '0'){//need to create a new entry in air times table so see if non half minute time exists
		$sqlB = "SELECT * FROM $airtimes_Table WHERE hour = '$hour' AND minute = '$minute' AND ampm = '$ampm'";
		$resultB = $db->getAll($sqlB, DB_FETCHMODE_ASSOC);
		 if (DB::isError($resultB)) {
		 die ($resultB->getMessage());
		 }
		$numB = count($resultB);


		if($numB > '0' && $seconds == '30'){
				$sqlC = "INSERT INTO $airtimes_Table values ('0','$hour','$minute','$seconds','$ampm','$military','" . $resultB[0][mon] . "','" . $resultB[0][tue] . "','" . $resultB[0][wed] . "','" . $resultB[0][thu] . "','" . $resultB[0][fri] . "','" . $resultB[0][sat] . "','" . $resultB[0][sun] . "')";
				$resultC = $db->query($sqlC);
				if (DB::isError($resultC)) {
					die ($resultC->getMessage());
				}

		}


	}


	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//END CHECK TO SEE IF MILITARY TIME EXISTS IN AIR_TIMES TABLE
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

							$mon = $_POST["mon$q"];
							if($mon != 'true' || !$mon){
							$mon = 'false';
							}
							$tue = $_POST["tue$q"];
							if($tue != 'true' || !$tue){
							$tue = 'false';
							}
							$wed = $_POST["wed$q"];
							if($wed != 'true' || !$wed){
							$wed = 'false';
							}
							$thu = $_POST["thu$q"];
							if($thu != 'true' || !$thu){
							$thu = 'false';
							}
							$fri = $_POST["fri$q"];
							if($fri != 'true' || !$fri){
							$fri = 'false';
							}
							$sat = $_POST["sat$q"];
							if($sat != 'true' || !$sat){
							$sat = 'false';
							}
							$sun = $_POST["sun$q"];
							if($sun != 'true' || !$sun){
							$sun = 'false';
							}


							$rate = ereg_replace ('[^0-9.]+', '', $_POST["rate$q"]);

							$live = $_POST["live$q"];
							if(!$live || $live == ""){
							$live = 'false';
							}
							$trade = $_POST["trade$q"];
							if(!$trade){
							$trade = 'false';
							}


							$alldata[] = array(0,$underwriter,$abbreviation,$_SESSION['AccurateTimeStamps'][$i][startTimeStamp],$_SESSION['AccurateTimeStamps'][$i][endTimeStamp],$length,$spotID,$hour,$minute,$seconds,$ampm,$military,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$type,$rate,$live,$trade);


					++$i;
					++$q;
			}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//BEGIN Conjur up method to split spots apart if they have more than one day asigned to them
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$TEMP_alldata = array();
for ($i = 0; $i < count($alldata); $i++) {

		if($alldata[$i][12] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'true','false','false','false','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][13] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','true','false','false','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][14] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','true','false','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][15] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','true','false','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][16] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','false','true','false','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][17] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','false','false','true','false',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}
		if($alldata[$i][18] == 'true'){
			$TEMP_alldata[] = array(0,$alldata[$i][1],$alldata[$i][2],$alldata[$i][3],$alldata[$i][4],$alldata[$i][5],$alldata[$i][6],$alldata[$i][7],$alldata[$i][8],$alldata[$i][9],$alldata[$i][10],$alldata[$i][11],'false','false','false','false','false','false','true',$alldata[$i][19],$alldata[$i][20],$alldata[$i][21],$alldata[$i][22]);
		}

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//END Conjur up method to split spots apart if they have more than one day asigned to them
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$sth = $db->prepare("INSERT INTO $grid_Table VALUES ('?',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$db->executeMultiple($sth, $TEMP_alldata);


			$_SESSION['message'] = "New grid item entered: $spotID";
			if($_POST['PlannerStamp']){
			$_SESSION['timestamp'] = $_POST['PlannerStamp'];
			header("Location:index.php?ID=0&subMenu=2&action=planner");
			}else{
			header("Location:index.php?ID=0&subMenu=2&action=enter");
			}

			unset($_SESSION['plannerEntry']);//empty array with entry info
			unset($_SESSION['displayConflicts']);//empty out conflict warnings for verify entries
			unset($_SESSION['keepUW']);
			unset($_SESSION['startMonth']);
			unset($_SESSION['startDay']);
			unset($_SESSION['startYear']);
			unset($_SESSION['endMonth']);
			unset($_SESSION['endDay']);
			unset($_SESSION['endYear']);
			unset($_SESSION['length']);
			unset($_SESSION['spotID']);
			unset($_SESSION['type']);
			unset($_SESSION['viewSpots']);
			unset($_SESSION['AccurateTimeStamps']);

			}
			//+++++++++++++++++++++++++
			//END ENTER GRID ITEM
			//+++++++++++++++++++++++++

//#########################################################################################

			//+++++++++++++++++++++++++
			//BEGIN MODIFY GRID ITEM
			//+++++++++++++++++++++++++
			if($action == 'modifyGrid'){
			 $highnumber = $_POST['highnumber'];
			unset($_SESSION['updateArray']);//	empty update array
			$_SESSION['updateArray'][] = array();


$alldata = array();


	$i=0;
	$q = 0;
	while($i < $highnumber){
	$processID = $_POST["radioGroup$i"];

		if($processID){
		$haveProcessedSomething = 'true';

		$action = substr("$processID", 0, (strpos($processID, ':')));
		$actionID = substr("$processID", ((strpos($processID, ':'))+1), strlen ("$processID"));
			//Delete Selected entries+++++
			if($action == 'delete'){
			$haveDeletedSomething = 'true';
			$alldata[] = array($actionID);
			}else if ($action == 'update'){
			$haveUpdatedSomething = 'true';
			//add entries to update to an array
			$_SESSION['updateArray']["$q"] = $actionID;
			++$q;
			}

	}

	++$i;
	}


$sth = $db->prepare("DELETE FROM $grid_Table WHERE ID = ?");
$db->executeMultiple($sth, $alldata);


	if($haveProcessedSomething != 'true'){
	$_SESSION['message'] = "NO ACTION TAKEN. You must select grid item(s) to update or delete!";
	header("Location:index.php?ID=0&subMenu=2&action=view");
	}else{
		if($haveDeletedSomething == 'true' && $haveUpdatedSomething != 'true'){
			$_SESSION['message'] = "The selected grid item(s) have been removed!";
			$_SESSION['timestamp'] = $_POST['maintainViewStamp'];
			header("Location:index.php?ID=0&subMenu=2&action=view");
		}else if($haveUpdatedSomething == 'true'){
		$_SESSION['timestamp'] = $_POST['maintainViewStamp'];
		$_SESSION['searchSubscriptions'] = 'true';
		$_SESSION['highNumber'] = $i;
		$_SESSION['message'] = "Here are the grid item(s) to update!";
		header("Location:index.php?ID=0&subMenu=2&action=modifyGrid");
		}


	}

			}
			//+++++++++++++++++++++++++
			//END MODIFY GRID ITEM
			//+++++++++++++++++++++++++


			//+++++++++++++++++++++++++
			//BEGIN VIEW GRID ITEM
			//+++++++++++++++++++++++++
			if($action == 'viewGrid' || $action == 'viewPlanner'){
				$startMonth = $_POST['startMonth'];
				$startDay = $_POST['startDay'];
				$startYear = $_POST['startYear'];
				$dateStamp = mktime(0,0,0,$startMonth,$startDay,$startYear);

				function findNearestMonday($day,$startDay,$startMonth,$startYear){

				//Find Last Weeks Monday if day is a sunday
					if($day == '0'){
					$monday = ($startDay - 6);
						//Get monday for past months except for january
						if($monday < '0' && $startMonth != '1'){
						$lastMonthsmonday = (date("t",mktime(0,0,0,($startMonth - 1),$startDay,$startYear)) + $monday);
						$_SESSION['timestamp'] = mktime(0,0,0,($startMonth - 1),$lastMonthsmonday,$startYear);

						//January - go to December for nearest monday
						}else if($monday < '0' && $startMonth == '1'){
						$lastMonthsmonday = (date("t",mktime(0,0,0,12,$startDay,($startYear - 1))) + $monday);
						$_SESSION['timestamp'] = mktime(0,0,0,12,$lastMonthsmonday,($startYear - 1));
						}else{
						$_SESSION['timestamp'] = mktime(0,0,0,$startMonth,$monday,$startYear);
						}


					//Find This Weeks Monday
					}else{
					$monday = ($startDay - $day) + 1;
					$_SESSION['timestamp'] = mktime(0,0,0,$startMonth,$monday,$startYear);
					}
					global $action;

					//split up for planner or for view grid
					if($action == 'viewGrid'){
						header("Location:index.php?ID=0&subMenu=2&action=view");
					}else if($action == 'viewPlanner'){
						header("Location:index.php?ID=0&subMenu=2&action=planner");
					}


				}

				findNearestMonday(date("w", $dateStamp),$startDay,$startMonth,$startYear);


			}
			//+++++++++++++++++++++++++
			//END VIEW GRID ITEM
			//+++++++++++++++++++++++++


//######################################################################################################################

			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN SEND ITEMS TO ENTRY FORM FROM GRID PLANNER
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			if($action=='plannerEntry'){
			$form_fields = array_keys($_POST);

			if(count($_SESSION['plannerEntry']) > 0){//holds info fron weekly planner
				//$_SESSION['plannerEntry'] = array_unique ($_SESSION['plannerEntry']);
				$startNumber = count($_SESSION['plannerEntry']);
			}else{
				$_SESSION['plannerEntry'] = array();
				$startNumber = 0;
			}

				for ($i = 0; $i < (sizeof($form_fields)-3); $i++) {
					$myPointer = $startNumber + $i;//add to stuff already in array
					//echo $startNumber . "<P>";
					$thisField = $form_fields[$i];
					$thisValue = $_POST[$thisField];

					  $inArray = false;
					//+++++++++++++++++++++++++++++++++++++++++++++++++++
					//BEGIN search array to see if military time already exists
					if(count($_SESSION['plannerEntry']) > 0){
						for ($m = 0; $m < count($_SESSION['plannerEntry']); $m++) {
							if (array_search($thisField, $_SESSION['plannerEntry'][$m],true) !== FALSE) {//military time already in array
    							$inArray = true;
								$locateID = $m;
								break;
							}

						}//end for loop


						//echo $_SESSION['plannerEntry'][$locateID][military];

					}//end if statmnt
					//END search to see if military time already exists
					//++++++++++++++++++++++++++++++++++++++++++++++++++++




					if($inArray != true){
						$_SESSION['plannerEntry'][$myPointer][military] = $thisField;
							for ($q = 0; $q < count($thisValue); $q++) {
								$_SESSION['plannerEntry'][$myPointer][days][] = $thisValue[$q];
							}
					}else{//update existing times with new days
							for ($q = 0; $q < count($thisValue); $q++) {
							array_push($_SESSION['plannerEntry'][$locateID][days],$thisValue[$q]);
								//$_SESSION['plannerEntry'][$locateID][days] => $thisValue[$q];
							}
						$_SESSION['plannerEntry'][$locateID][days] = array_unique($_SESSION['plannerEntry'][$locateID][days]);
					}



				}//end main for loop


				//+++++++++++++++++++++++++++++
				//SORT array by military keys
				//++++++++++++++++++++++++++++++
   				$my_array = multi_sort($_SESSION['plannerEntry'], "military", true);
				$_SESSION['plannerEntry'] = $my_array;



					$_SESSION['PlannerStamp'] = $_POST['PlannerStamp'];
					header("Location:index.php?ID=0&subMenu=2&action=enter");

			//$_SESSION['plannerEntry'] = array();
			//echo $_SESSION['plannerEntry'][0][military];
			//print_r($my_array);

			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//END  SEND ITEMS TO ENTRY FORM FROM GRID PLANNER
			//+++++++++++++++++++++++++++++++++++++++++++++++++



//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@----SWITCHBOARD SECTION----@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

if($action=='switchBoard'){
	$_SESSION['updateArray'] = array();
	$_SESSION['highNumber'] = 0;
//########################################################################################
//BEGIN MOVES SECTION#####################################################################
//########################################################################################

 	if($_POST['movesTotal'] > '0'){

	//initialize Stuff
	$targetSlotStorage = array();
	$sourceSlotStorage = array();
	$incomingChanges = array();
	$deletedata = array();
	$insertNewdata = array();
	$updatedata = array();
	$schedulingConflicts = "";
	$schedulingMoves = "";
	$moveMessage = "";


	for ($i = 0; $i < $_POST['movesTotal']; $i++) {

	 	$deleteTarget = 'true';
		$pieces = explode(":", $_POST["move$i"]);
		$incomingChanges[] = $pieces;

		if($pieces[1] < 13){
			$hour = $pieces[1];
			$ampm = "AM";
		}else{
			$hour = (24 - $pieces[1]);
			$ampm = "PM";
		}
		$minute = $pieces[2];
		$seconds = $pieces[3];
		$military = $pieces[1] . ":" . $pieces[2] . ":" . $pieces[3];
		$day = array_search ("true", $pieces);
		if($day == 4){
			$day = "mon";
			$dow = '1';
		}else if($day == 5){
			$day = "tue";
			$dow = '2';
		}else if($day == 6){
			$day = "wed";
			$dow = '3';
		}else if($day == 7){
			$day = "thu";
			$dow = '4';
		}else if($day == 8){
			$day = "fri";
			$dow = '5';
		}else if($day == 9){
			$day = "sat";
			$dow = '6';
		}else if($day == 10){
			$day = "sun";
			$dow = '0';
		}

		//Harvest destination slots info in case it is needed later.
 		 $sql = "SELECT * FROM $grid_Table WHERE military = '$military' AND $day = 'true'";
		 $targetSlotStorage[$i] = $db->getAll($sql, DB_FETCHMODE_ASSOC);
		 if (DB::isError($targetSlotStorage[$i])) {
		 die ($targetSlotStorage[$i]->getMessage());
		 }

		 //Harvest source slot info
 		 $sql = "SELECT * FROM $grid_Table WHERE ID = '".$pieces[0]."'";
		 $sourceSlotStorage[$i] = $db->getAll($sql, DB_FETCHMODE_ASSOC);
		 if (DB::isError( $sourceSlotStorage[$i])) {
		 die ( $sourceSlotStorage[$i]->getMessage());
		 }


		$make_move = "";
		$make_update = "";
		//Dtermine new start and end time stamps. returns $_SESSION['AccurateStartStamp'],$_SESSION['AccurateEndStamp']
		determineTimeStamps($dow,$_SESSION['timestamp'],$sourceSlotStorage[$i][0]['endTimeStamp']);

		 	for($e = 0; $e < count($targetSlotStorage[$i]); $e++){
				if(($targetSlotStorage[$i][$e]['startTimeStamp'] > $_SESSION['AccurateEndStamp'] && $_SESSION['AccurateEndStamp'] != 'NA') || ($targetSlotStorage[$i][$e]['endTimeStamp'] != 'NA' && $targetSlotStorage[$i][$e]['endTimeStamp'] < $_SESSION['AccurateStartStamp'])){//assuming there is a future spot
					//no scheduling conflicts, make the move
					$make_move = 'true';
				}elseif($_SESSION['AccurateEndStamp'] == 'NA' && $targetSlotStorage[$i][$e]['startTimeStamp'] >= ($_SESSION['timestamp']+518400)){//send it to be updated b/c there is a future spot scheduled
					//scheduling conflict
					$make_update = 'true';
				}else{
					//delete this target spot
				 	$deletedata[] = array($targetSlotStorage[$i][$e]['ID']);
				}
			}



		 	if(($make_move == 'true' && $make_update == "") || ($make_move == "" && $make_update == "")){
		 		$updatedata[] = array($_SESSION['AccurateStartStamp'],$_SESSION['AccurateEndStamp'],$hour,$minute,$seconds,$ampm,$military,$pieces[4],$pieces[5],$pieces[6],$pieces[7],$pieces[8],$pieces[9],$pieces[10],$pieces[0]);
			}else{//send it to be updated
				$_SESSION['updateArray'][] = $pieces[0];
				$_SESSION['highNumber']++;
				$schedulingConflicts = $schedulingConflicts . "<BR>&nbsp;&#8226; " . $sourceSlotStorage[$i][0]['spotID'];
			}



			$schedulingMoves = $schedulingMoves . "<BR>&nbsp;&#8226; " . $sourceSlotStorage[$i][0]['spotID'] . " at $hour:$minute:$seconds $ampm on $day";
			//prepare info to Delete target info
		 	//$deletedata[] = array($targetSlotStorage[$i][0]['ID']);
			//prepare info to insert new entry with updated info
            //if(isset($targetSlotStorage[$i][0][ID])){//something exists in the target slot therefore we will need to insert a new entry
			//$insertNewdata[] = array(0,$sourceSlotStorage[$i][0]['underwriter'],$sourceSlotStorage[$i][0]['abbreviation'],$_SESSION['accurateStartTime'],$_SESSION['accurateEndTime'],$sourceSlotStorage[$i][0]['length'],$sourceSlotStorage[$i][0]['spotID'],$hour,$minute,$seconds,$ampm,$military,$pieces[4],$pieces[5],$pieces[6],$pieces[7],$pieces[8],$pieces[9],$pieces[10],$sourceSlotStorage[$i][0]['type'],$sourceSlotStorage[$i][0]['rate'],$sourceSlotStorage[$i][0]['live'],$sourceSlotStorage[$i][0]['trade']);
		 	//$deletedata[] = array($sourceSlotStorage[$i][0]['ID']);
			//}else{
			//}
		 //}




	}//end loop

	  if(count($_SESSION['updateArray']) > '0'){//stuff to send to update form
		$updateMessage = "<BR><font color=\"#000000\">The following spot(s) have possible scheduling conflicts and must be manually changed:</font>$schedulingConflicts";
		$_SESSION['backtogrid'] = 'true';
		$somethingToUpdate = 'true';
	}



		  //Update spots
		  if(count($updatedata) > '0'){
		   $sth = $db->prepare("UPDATE $grid_Table SET startTimeStamp = ?, endTimeStamp = ?, hour = ?,minute = ?,seconds = ?,ampm = ?,military = ?,mon = ?,tue = ?,wed = ?,thu = ?,fri = ?,sat = ?,sun = ? WHERE ID = ?");
		   $db->executeMultiple($sth, $updatedata);
		   $moveMessage = "<BR><font color=\"#000000\">The following spot(s) have been moved:</font>$schedulingMoves";
		  }


		  //Don't delete items that have been updated
		  //$new_deletedata[] = array_diff ($deletedata, array array2 [, array ...])


//array_diff() returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are preserved.

//echo $updatedata[0][14];

		// Don't delete an ID if it was one that has already been moved...
		$new_update_array = array();
		for ($i = 0; $i < count($updatedata); $i++) {
			$new_update_array[] = $updatedata[$i][14];
		}

		for ($i = 0; $i < count($deletedata); $i++) {
			if(in_array ($deletedata[$i][0], $new_update_array) == FALSE){
			//echo $deletedata[$i][0] . " - " . $new_update_array[$i] ."<BR>";
				$new_deletedata[]=$deletedata[$i][0];
			}

		}


		//delete the target slot info
		  if(count($new_deletedata) > '0'){
			$sql = $db->prepare("DELETE FROM $grid_Table WHERE ID = ?");
		  	$db->executeMultiple($sql, $new_deletedata);
          }



		  /* //Insert New Spots
		  if(count($insertNewdata) > '0'){
		   $sth = $db->prepare("INSERT INTO $grid_Table VALUES ('?',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		   $db->executeMultiple($sth, $insertNewdata);
		   $moveMessage = "<BR><font color=\"#000000\">The following spot(s) have been moved:</font>$schedulingMoves";
		  }  */



}//end if($_POST['movesTotal'] > '0'){

//########################################################################################
//END MOVES SECTION#######################################################################
//########################################################################################



//########################################################################################
//BEGIN COPIES SECTION####################################################################
//########################################################################################

 	if($_POST['copyTotal'] > '0'){


	//initialize Stuff
	$targetSlotStorage = array();
	$sourceSlotStorage = array();
	$incomingChanges = array();
	$deletedata = array();
	$insertNewdata = array();
	$updatedata = array();
	$schedulingCopies = "";
	$copyMessage = "";


	for ($i = 0; $i < $_POST['copyTotal']; $i++) {

	 	$deleteTarget = 'true';
		$pieces = explode(":", $_POST["copy$i"]);
		$incomingChanges[] = $pieces;

		if($pieces[1] < 13){
			$hour = $pieces[1];
			$ampm = "AM";
		}else{
			$hour = (24 - $pieces[1]);
			$ampm = "PM";
		}
		$minute = $pieces[2];
		$seconds = $pieces[3];
		$military = $pieces[1] . ":" . $pieces[2] . ":" . $pieces[3];
		$day = array_search ("true", $pieces);
		if($day == 4){
			$day = "mon";
			$dow = '1';
		}else if($day == 5){
			$day = "tue";
			$dow = '2';
		}else if($day == 6){
			$day = "wed";
			$dow = '3';
		}else if($day == 7){
			$day = "thu";
			$dow = '4';
		}else if($day == 8){
			$day = "fri";
			$dow = '5';
		}else if($day == 9){
			$day = "sat";
			$dow = '6';
		}else if($day == 10){
			$day = "sun";
			$dow = '0';
		}

		//Harvest destination slots info in case it is needed later.
 		 $sql = "SELECT * FROM $grid_Table WHERE military = '$military' AND $day = 'true'";
		 $targetSlotStorage[$i] = $db->getAll($sql, DB_FETCHMODE_ASSOC);
		 if (DB::isError($targetSlotStorage)) {
		 die ($targetSlotStorage->getMessage());
		 }

		 //Harvest source slot info
 		 $sql = "SELECT * FROM $grid_Table WHERE ID = '".$pieces[0]."'";
		 $sourceSlotStorage[$i] = $db->getAll($sql, DB_FETCHMODE_ASSOC);
		 if (DB::isError($sourceSlotStorage)) {
		 die ($sourceSlotStorage->getMessage());
		 }




		$make_move = "";
		$make_update = "";
		//Dtermine new start and end time stamps. returns $_SESSION['AccurateStartStamp'],$_SESSION['AccurateEndStamp']
		determineTimeStamps($dow,$_SESSION['timestamp'],$sourceSlotStorage[$i][0]['endTimeStamp']);

		 	for($e = 0; $e < count($targetSlotStorage[$i]); $e++){
				if(($targetSlotStorage[$i][$e]['startTimeStamp'] > $_SESSION['AccurateEndStamp'] && $_SESSION['AccurateEndStamp'] != 'NA') || ($targetSlotStorage[$i][$e]['endTimeStamp'] != 'NA' && $targetSlotStorage[$i][$e]['endTimeStamp'] < $_SESSION['AccurateStartStamp'])){//assuming there is a future spot
					//no scheduling conflicts, make the move
					$make_move = 'true';
				}elseif($_SESSION['AccurateEndStamp'] == 'NA' && $targetSlotStorage[$i][$e]['startTimeStamp'] >= ($_SESSION['timestamp']+518400)){//send it to be updated b/c there is a future spot scheduled
					//scheduling conflict
					$make_update = 'true';
				}else{
					//delete this target spot
				 	$deletedata[] = array($targetSlotStorage[$i][$e]['ID']);
				}
			}



		 	if(($make_move == 'true' && $make_update == "") || ($make_move == "" && $make_update == "")){
				//prepare info to insert new entry with updated info
				$insertNewdata[] = array(0,$sourceSlotStorage[$i][0]['underwriter'],$sourceSlotStorage[$i][0]['abbreviation'],$_SESSION['AccurateStartStamp'],$_SESSION['AccurateEndStamp'],$sourceSlotStorage[$i][0]['length'],$sourceSlotStorage[$i][0]['spotID'],$hour,$minute,$seconds,$ampm,$military,$pieces[4],$pieces[5],$pieces[6],$pieces[7],$pieces[8],$pieces[9],$pieces[10],$sourceSlotStorage[$i][0]['type'],$sourceSlotStorage[$i][0]['rate'],$sourceSlotStorage[$i][0]['live'],$sourceSlotStorage[$i][0]['trade']);
			}else{//send it to be updated
				$_SESSION['updateArray'][] = $pieces[0];
				$_SESSION['highNumber']++;
				$schedulingConflicts = $schedulingConflicts . "<BR>&nbsp;&#8226; " . $sourceSlotStorage[$i][0]['spotID'];
			}








		/*  //if there is only one or less entries for the target time slot it is OK to delete what's in the target slot
		 if(count($targetSlotStorage[$i]) <= '1'){
		 	$deleteTarget = 'true';
		 //else find out if the target entries have end dates or not... if no end date it's OK to delete
		 }else{
		    $deleteTarget = 'false';
		 }//end else


		 if($deleteTarget == 'false'){//send to be updated manually
		 		$_SESSION['updateArray'][] = $pieces[0];
				$_SESSION['highNumber']++;
				$schedulingConflicts = $schedulingConflicts . "<BR>&nbsp;&#8226; " . $sourceSlotStorage[$i][0]['spotID'];
		 }else{//go ahead and make the move...

				//Dtermine new start and end time stamps. returns $_SESSION['AccurateStartStamp'],$_SESSION['AccurateEndStamp']
				determineTimeStamps($dow,$_SESSION['timestamp'],$sourceSlotStorage[$i][0]['endTimeStamp']);


			//prepare info to Delete target info
		 	$deletedata[] = array($targetSlotStorage[$i][0][ID]);

			//prepare info to insert new entry with updated info
			$insertNewdata[] = array(0,$sourceSlotStorage[$i][0]['underwriter'],$sourceSlotStorage[$i][0]['abbreviation'],$_SESSION['AccurateStartStamp'],$_SESSION['AccurateEndStamp'],$sourceSlotStorage[$i][0]['length'],$sourceSlotStorage[$i][0]['spotID'],$hour,$minute,$seconds,$ampm,$military,$pieces[4],$pieces[5],$pieces[6],$pieces[7],$pieces[8],$pieces[9],$pieces[10],$sourceSlotStorage[$i][0]['type'],$sourceSlotStorage[$i][0]['rate'],$sourceSlotStorage[$i][0]['live'],$sourceSlotStorage[$i][0]['trade']);

		 } */



		$schedulingCopies = $schedulingCopies . "<BR>&nbsp;&#8226; " . $sourceSlotStorage[$i][0]['spotID'] . " at $hour:$minute:$seconds $ampm on $day";

	}//end loop

 	if(count($_SESSION['updateArray']) > '0'){//stuff to send to update form
		$updateMessage = "<BR><font color=\"#000000\">The following spots have possible scheduling conflicts and must be manually changed:</font>$schedulingConflicts";
		$_SESSION['backtogrid'] = 'true';
		$somethingToUpdate = 'true';
	}

		  //Insert New Spots
 		  if(count($insertNewdata) > '0'){
		   $sth = $db->prepare("INSERT INTO $grid_Table VALUES ('?',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		   $db->executeMultiple($sth, $insertNewdata);
		   $copyMessage = "<BR><font color=\"#000000\">The following spot(s) have been copied:</font>$schedulingCopies";
		  }

		 	//delete the target slot info
		  if(count($deletedata) > '0'){
			$sql = $db->prepare("DELETE FROM $grid_Table WHERE ID = ?");
		  	$db->executeMultiple($sql, $deletedata);
          }

}//end if($_POST['copiesTotal'] > '0'){

//########################################################################################
//END COPIES SECTION######################################################################
//########################################################################################




//########################################################################################
//BEGIN DELETES SECTION###################################################################
//########################################################################################
if($_POST['deletesTotal'] > 0){
	$alldata = array();
	$updatedSpots = array();
	$myDisplaySpots = "";

	for ($i = 0; $i < $_POST['deletesTotal']; $i++) {


		$alldata[] = array($_POST["delete$i"]);

		$sql = "SELECT spotID,hour,minute,seconds,ampm FROM $grid_Table WHERE ID = '" . $_POST["delete$i"] . "'";
		$result = $db->getAll($sql,DB_FETCHMODE_ASSOC);
		if (DB::isError($result)) {
		die ($result->getMessage());
		}



		$myDisplaySpots = $myDisplaySpots . "<BR>&nbsp;&#8226; " . $result[0]['spotID'] . " at ". $result[0]['hour'] . ":" . $result[0]['minute'] . ":" . $result[0]['seconds'] . " " . $result[0]['ampm'] ." on " . $_POST["deleteDays$i"];
	}

	$sth = $db->prepare("DELETE FROM $grid_Table WHERE ID = ?");
	$db->executeMultiple($sth, $alldata);
	$somethingDeleted = 'true';
	$deleteMessage = "<BR><font color=\"#000000\">The following spot(s) have been deleted:</font>$myDisplaySpots";


}

//########################################################################################
//END DELETES SECTION#####################################################################
//########################################################################################

//########################################################################################
//BEGIN UPDATES SECTION###################################################################
//########################################################################################
if($_POST['updatesTotal'] > 0){
	$_SESSION['updateArray'] = array();


	for ($i = 0; $i < $_POST['updatesTotal']; $i++) {
		$_SESSION['updateArray'][] = $_POST["update$i"];
	}

		$_SESSION['highNumber'] = ($_SESSION['highNumber'] + $_POST['updatesTotal']);
		$updateMessage = $updateMessage . "<BR><font color=\"#000000\">Below are the grid item(s) you requested to update:";
		$_SESSION['backtogrid'] = 'true';
		$somethingToUpdate = 'true';


}
//########################################################################################
//END UPDATES SECTION#####################################################################
//########################################################################################


	$_SESSION['message'] = $moveMessage . $copyMessage . $deleteMessage . $updateMessage;

   if($somethingToUpdate == 'true'){
	header("Location:index.php?ID=0&subMenu=2&action=modifyGrid");
}else{
	header("Location:index.php?ID=0&subMenu=2&action=planner");
}




}//end if($action=='switchBoard'){




//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@----DAILY PROGRAM LOG SECTION----@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@




			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN ENTER RECURRING PROGRAMS SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			if($action=='enterPrograms'){
			//echo " TEST";
				$programTitle = mysql_real_escape_string (strtoupper(trim($_POST['title'])));
				$hour = $_POST['hour'];
				$minute = $_POST['minute'];
				$seconds = $_POST['seconds'];
				$ampm = $_POST['ampm'];
				calcMilitary($hour,$minute,$seconds,$ampm);
				$startTimeStamp = mktime (0,0,0,$_POST['startMonth'],$_POST['startDay'],$_POST['startYear']);
				$endTimeStamp = mktime (0,0,0,$_POST['endMonth'],$_POST['endDay'],$_POST['endYear']);
				$mon = $_POST['mon'];
				$tue = $_POST['tue'];
				$wed = $_POST['wed'];
				$thu = $_POST['thu'];
				$fri = $_POST['fri'];
				$sat = $_POST['sat'];
				$sun = $_POST['sun'];

				$source = $_POST['source'];
				$source_other = mysql_real_escape_string (trim($_POST['source_other']));
				$type = $_POST['type'];
				$type_other = mysql_real_escape_string(trim($_POST['type_other']));

				if($source_other == 'Enter Optional Description:' || $source != 'Other'){
					$source_other = "";
				}
				if($type_other == 'Enter Optional Description:' || $type != 'Other'){
					$type_other = "";
				}



				$sql = "INSERT into $programTitles_Table values ('0','$programTitle','$military','$startTimeStamp','$endTimeStamp','$mon','$tue','$wed','$thu','$fri','$sat','$sun','$source','$source_other','$type','$type_other','')";
				$result = $db->query($sql);
				if (DB::isError($result)) {
				die ($result->getMessage());
				}
				$_SESSION['message'] = "The program, " .  stripslashes(stripslashes($programTitle)) .", was successfully added!";
				header("Location:index.php?ID=1&subMenu=0&action=enterPrograms");
			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//END ENTER RECURRING PROGRAMS SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++







			//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN UPDATE OR DELETE PROGRAMS SECTION (FROM LIST VIEW)
			//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			if($action == 'modifyPrograms'){
			$_SESSION['fromList'] = 'true';
			if(count($_SESSION['updateArray']) > '0'){
				unset($_SESSION['updateArray']);
			}
					$highnumber = $_POST['highnumber'];
					$_SESSION['updateArray'][] = array();
					$i=0;
					$q = 0;
					while($i < $highnumber){
					$processID = $_POST["radioGroup$i"];
						if($processID){
						$haveProcessedSomething = 'true';
						$action = substr("$processID", 0, (strpos($processID, ':')));
						$actionID = substr("$processID", ((strpos($processID, ':'))+1), strlen ("$processID"));
							//Delete Selected entries+++++
							if($action == 'delete'){
							$haveDeletedSomething = 'true';

							$result[] = $db->query("DELETE from $programTitles_Table WHERE ID = '$actionID'");
							}else if ($action == 'update'){
							$haveUpdatedSomething = 'true';
							//add entries to update to an array
							$_SESSION['updateArray'][$q] = $actionID;
							++$q;
							}

					}

					++$i;
					}
					if($haveProcessedSomething != 'true'){
					$_SESSION['message'] = "NO ACTION TAKEN. You must select program(s) to update or delete!";
					header("Location:index.php?ID=1&subMenu=0&action=listPrograms");

					}else{
						if($haveDeletedSomething == 'true' && $haveUpdatedSomething != 'true'){
							$_SESSION['message'] = "The selected program(s) have been removed!";
							header("Location:index.php?ID=1&subMenu=0&action=listPrograms");
						}else if($haveUpdatedSomething == 'true'){
						$_SESSION['searchSubscriptions'] = 'true';
						//$_SESSION['highNumber'] = $i;
						$_SESSION['message'] = "Here are the program(s) to update!";
						header("Location:index.php?ID=1&subMenu=0&action=modifyPrograms");
						//echo count($_SESSION['updateArray']);
						}

					}
					//end main if statement for List functions
			}
			//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			//END UPDATE OR DELETE PROGRAMS SECTION (FROM LIST VIEW)
			//++++++++++++++++++++++++++++++++++++++++++++++++++++++++





			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN UPDATE PROGRAMS SECTION (FROM Modify Programs View)
			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			if($action == 'updatePrograms'){
			$alldata = array();
			$updates = "";
			for ($r = 0; $r < $_POST['highNumber']; $r++) {
				$ID = $_POST["ID$r"];
				$programTitle = mysql_real_escape_string (stripslashes(stripslashes(strtoupper(trim($_POST["title$r"])))));
				if($programTitle == ""){
					$programTitle = "&nbsp;";
				}
				$updates = "<BR>&#8226; " . stripslashes(stripslashes($programTitle)) . $updates;
				$hour = $_POST["hour$r"];
				$minute = $_POST["minute$r"];
				$seconds = $_POST["seconds$r"];
				$ampm = $_POST["ampm$r"];
				calcMilitary($hour,$minute,$seconds,$ampm);
				$startTimeStamp = mktime (0,0,0,$_POST["startMonth$r"],$_POST["startDay$r"],$_POST["startYear$r"]);
				$endTimeStamp = mktime (0,0,0,$_POST["endMonth$r"],$_POST["endDay$r"],$_POST["endYear$r"]);
				$mon = $_POST["mon$r"];
				$tue = $_POST["tue$r"];
				$wed = $_POST["wed$r"];
				$thu = $_POST["thu$r"];
				$fri = $_POST["fri$r"];
				$sat = $_POST["sat$r"];
				$sun = $_POST["sun$r"];
				$source = $_POST["source$r"];
				if($source == ""){
					$source = "&nbsp;";
				}
				$source_other = mysql_real_escape_string (trim($_POST["source_other$r"]));
				$type = $_POST["type$r"];
				if($type == ""){
				 $type ="&nbsp;";
				}
				$type_other = mysql_real_escape_string (trim($_POST["type_other$r"]));
				if($source != 'Other'){
					$source_other = "";
				}
				if($type != 'Other'){
					$type_other = "";
				}


				$alldata[] = array($programTitle,$military,$startTimeStamp,$endTimeStamp,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$source,$source_other,$type,$type_other,$ID);
			}

			$sth = $db->prepare("UPDATE $programTitles_Table SET title = ?,military = ?,startTimeStamp = ?,endTimeStamp = ?,mon =?,tue = ?,wed=?,thu=?,fri = ?,sat = ?,sun = ?,source = ?,source_other = ?,type = ?,type_other = ? WHERE ID = ?");
			$db->executeMultiple($sth, $alldata);

			unset($_SESSION['updateArray']);
			//print_r($alldata);
			$_SESSION['message'] = "<BR>Your have successfully updated the following programs:$updates";

			if($_POST['fromLog'] == 'true'){
				$_SESSION['message'] = "<BR>Your have successfully updated the following programs:$updates";
				unset($_SESSION['fromList']);
				$_SESSION['anchor'] = $_POST['anchor'];
				header("Location:index.php?ID=1&subMenu=1#".$_POST['anchor']."");
			}elseif($_SESSION['fromList'] == 'true'){
				unset($_SESSION['fromList']);
				header("Location:index.php?ID=1&subMenu=0&action=listPrograms");
			}else{
				unset($_SESSION['fromList']);
				header("Location:index.php?ID=1&subMenu=0&action=modifyPrograms");
			}

	}
			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			//END UPDATE PROGRAMS SECTION (FROM Modify Programs View)
			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN SEARCH PROGRAMS SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			if($action == 'ProgramSearch'){
					$searchName = mysql_real_escape_string (strtoupper(trim($_POST['name'])));
						$sql = "SELECT * FROM $programTitles_Table WHERE title LIKE '%$searchName%' ORDER BY title asc";
						$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
						if (DB::isError($result)) {
						die ($result->getMessage());
						}


					$num = count($result);
					if($num=='0'){
					$_SESSION['message'] = "No Matches Found!";
					header("Location:index.php?ID=1&subMenu=0&action=searchPrograms");
					}else{

					$_SESSION['searchArray'][] = array();
					$i=0;
					while ($i < count($result)) {
						$_SESSION['searchArray'][$i] = $result[$i]['ID'];
						++$i;
					}
					$_SESSION['searchPrograms'] = 'true';
					$_SESSION['message'] = "Here are your search results!";
					header("Location:index.php?ID=1&subMenu=0&action=listPrograms");
				}

			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			//END SEARCH PROGRAMS SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++





			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN QUICK ADD SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			if($action=='quick_add'){

				$programTitle = mysql_real_escape_string (strtoupper(trim($_POST['title'])));
				$hour = $_POST['hour'];
				$minute = $_POST['minute'];
				$seconds = $_POST['seconds'];
				$ampm = $_POST['ampm'];
				calcMilitary($hour,$minute,$seconds,$ampm);


				//$_SESSION['log_timestamp']
				if($_POST['permanent'] == 'true'){
					$startTimeStamp = $_SESSION['log_timestamp'];
					$endTimeStamp = '-1';//This is a permanent entry with no end date
				}else{
					$startTimeStamp = $_SESSION['log_timestamp'];
					$endTimeStamp = $_SESSION['log_timestamp'];
				}

				$days = array($_POST['mon'],$_POST['tue'],$_POST['wed'],$_POST['thu'],$_POST['fri'],$_POST['sat'],$_POST['sun']);


				if($_POST['blank'] == 'true'){
					$programTitle = "&nbsp;";
					$source = "&nbsp;";
					$type = "&nbsp;";
				}else{
					$source = $_POST['source'];
					$type = $_POST['type'];
				}


				$source_other = "";
				$type_other = "";



				$sql = "INSERT into $programTitles_Table values ('0','$programTitle','$military','$startTimeStamp','$endTimeStamp','".$days[0]."','".$days[1]."','".$days[2]."','".$days[3]."','".$days[4]."','".$days[5]."','".$days[6]."','$source','$source_other','$type','$type_other','')";
				$result = $db->query($sql);
				if (DB::isError($result)) {
				die ($result->getMessage());
				}
				$_SESSION['message'] = "The program, " .  stripslashes(stripslashes($programTitle)) .", was successfully added!";
				$_SESSION['anchor'] = ($_POST['Myanchor']+1);
				header("Location:index.php?ID=1&subMenu=1#".($_POST['Myanchor'] +1)."");
			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//end QUICK ADD SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++

			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN QUICK UPDATE SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			if(isset($_POST['quick_update'])){
			//echo "SPAM";
				//header("Location:index.php?ID=1&subMenu=0&action=modifyPrograms&programID=".$_POST['quick_update']."");
			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN QUICK UPDATE SECTION
			//++++++++++++++++++++++++++++++++++++++++++++++++


			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN QUICK DELETE SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			if($action=='quick_delete'){
				$sql = "DELETE FROM $programTitles_Table WHERE ID ='".$_POST['deleteID']."'";
				$result = $db->query($sql);
				if (DB::isError($result)) {
				die ($result->getMessage());
				}
				$_SESSION['message'] = "The selected entry was successfully deleted!";
				$_SESSION['anchor'] = $_POST['anchor'];
				header("Location:index.php?ID=1&subMenu=1#".$_POST['anchor']."");
			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN QUICK DELETE SECTION
			//++++++++++++++++++++++++++++++++++++++++++++++++




			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN CHANGE PROGRAM STATUS SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			if($action=='change_Status'){

				$alldata[] = array($_POST['active'],$_POST['ID']);
				$sth = $db->prepare("UPDATE $programTitles_Table SET active = ? WHERE ID = ?");
				$db->executeMultiple($sth, $alldata);

				if($_POST['active'] == 'true'){
					$status = 'active';
				}else{
					$status = 'inactive';
				}
				$_SESSION['message'] = $_POST['title'] . " at ". $_POST['time'] . " was set to <u>$status</u>.";
				$_SESSION['anchor'] = $_POST['myAnchor'];
				header("Location:index.php?ID=1&subMenu=1#".$_POST['myAnchor']."");
			}
			//+++++++++++++++++++++++++++++++++++++++++++++++++
			//END CHANGE PROGRAM STATUS SECTION
			//+++++++++++++++++++++++++++++++++++++++++++++++++









//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@----ADMIN SECTION----@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


//################################################
//BEGIN UPDATE PRINT OPTIONS SECTION
//################################################
if($action=='plannerPrintOptions'){

	$alldata[] = array($_POST['client'],$_POST['spot'],$trade = $_POST['trade'],$_POST['rate'],$_POST['live'],$_POST['type'],$_POST['length'],$stationID);
	$sth = $db->prepare("UPDATE user_options SET planner_print_client = ?,planner_print_spot = ?,planner_print_trade = ?,planner_print_rate = ?,planner_print_live = ?,planner_print_type = ?,planner_print_length = ? WHERE station = ?");
	$db->executeMultiple($sth, $alldata);

	$_SESSION['message'] = "Your print options have successfully been updated!";
	header("Location:index.php?ID=5");
}
//################################################
//END UPDATE PRINT OPTIONS SECTION
//################################################




//################################################
//BEGIN DATABSE MANAGEMENT SECTION
//################################################
if($action=='DBManagement'){

$backupFile = "/var/www/html/traffic/backups/$stationID/weekly_planner_".date("m-d-y").".sql";
//$backupFile = "weekly_planner.sql";

$sql = "SELECT * INTO OUTFILE '$backupFile' FROM $grid_Table";
$result = $db->query($sql);
if (DB::isError($result)) {
die ($result->getMessage());
}


}
//################################################
//END DATABSE MANAGEMENT SECTION
//################################################




$db->disconnect();
?>
